package com.windoent.yugioh.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.ui.activity.registlogin.LoginActivity;
import com.windoent.yugioh.utils.LogUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.zhy.http.okhttp.callback.StringCallback;

/**
 * author : robert
 * date   : 2021/4/29 11:04
 * desc   :添加回调Activity
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d("onResp", "onResp: --->" + baseResp.getType() + ",code:" + baseResp.errCode);
        if (baseResp.getType() == 2) {//分享
            finish();
        }
        if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {//跳转小程序返回数据
            WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
            String extraData = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
            ToastUtils.showText("miaozhen-->:" + extraData);
            finish();
        }
        switch (baseResp.errCode) {//微信登录
            case BaseResp.ErrCode.ERR_OK:
                String code = ((SendAuth.Resp) baseResp).code;
                sendAliasBroadcastReceive(code);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                ToastUtils.showText("发送取消");
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                ToastUtils.showText("发送被拒绝");
                break;
            default:
                ToastUtils.showText("发送返回");
                break;
        }
        finish();
    }

    private void sendAliasBroadcastReceive(String code) {
        Intent aliasIntent = new Intent(Constants.MESSAGE_RECEIVED_ALIAS_ACTION);
        aliasIntent.putExtra(Constants.WEIXINCODE, code);
        LocalBroadcastManager.getInstance(this).sendBroadcast(aliasIntent);
    }
}
